﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBase : MonoBehaviour
{
    //基础
    public int hp = 1;
    public EnemyMode mode;
    public Vector3 spawnPos;
    public FaceDir dir;
    protected bool bIsDead;

    public GameObject enemyProject;
    public Transform projectSpawnPoint;

    //攻击
    public bool bCanAttack;
    public float attackCD;
    protected float attackTimeStamp;

    //速度
    public float HorizontalSpeed;
    public float VerticalOffsetRange;
    public float VerticalOffsetFrequency = 1f;

    public AudioClip AudioClipDie;

    protected Animator anim;
    protected Collider2D col2d;
    protected Rigidbody2D rb2d;
    protected AudioSource audioSource;

    protected void Awake()
    {
        anim = this.GetComponent<Animator>();
        col2d = this.GetComponent<Collider2D>();
        rb2d = this.GetComponent<Rigidbody2D>();
        audioSource = this.GetComponent<AudioSource>();
        VerticalOffsetRange = Random.Range(0f, 0.03f);
    }

    protected void Update()
    {
        Tick(Time.deltaTime);
    }

    protected void FixedUpdate()
    {
        FixedTick(Time.fixedDeltaTime);
    }
    
    protected void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.CompareTag("Player1Project"))
        {
            Kill();
        }
        else if (collision.transform.CompareTag("Player2Project"))
        {
            Kill();
        }
    }

    public virtual void Init(EnemyMode mode, Vector3 spawnPos, FaceDir dir)
    {
        this.mode = mode;
        this.spawnPos = spawnPos;
        this.dir = dir;

        //print("Base" + dir);

        //镜像
        if (dir == FaceDir.FACE_DIR_LEFT)
        {
            this.GetComponent<SpriteRenderer>().flipX = true;
            HorizontalSpeed = -HorizontalSpeed;
            projectSpawnPoint.localPosition = new Vector3(-projectSpawnPoint.localPosition.x, 0, 0);
        }
    }

    protected virtual void FixedTick(float fixedDeltaTime)
    {
        if (bIsDead)
        {
            return;
        }
    }

    protected virtual void Tick(float deltaTime)
    {
        if (bIsDead)
        {
            return;
        }
        CheckAttack();
    }

    protected void CheckAttack()
    {
        if (bCanAttack && Time.time >= attackTimeStamp + attackCD)
        {
            ExecAttack();
        }
    }

    protected virtual void ExecAttack()
    {
        anim.SetTrigger("Attack");
        GameObject enemyProjectObj = GameObject.Instantiate(enemyProject, projectSpawnPoint.position, Quaternion.Euler(Vector3.right));
        enemyProjectObj.GetComponent<EnemyProject>().InitProject(dir, this.gameObject);
        attackTimeStamp = Time.time;
    }

    protected void Kill()
    {
        if (bIsDead)
        {
            return;
        }
        bIsDead = true;
        anim.SetTrigger("Die");
        this.col2d.enabled = false;

        audioSource.clip = AudioClipDie;
        audioSource.Play();

        EventMgr.Call(Events.ON_ENEMY_KILLED, this.gameObject);
        GameObject.Destroy(this.gameObject, 1f);
    }
}